const commentDao = require("@/dao/comment");
const z_base_Dao = require("@/dao/z_base");
const CommentModal = require("@/model/Comment");
const { ErrorCode } = require("@/utils/r/code");
const { cloneDeep } = require("lodash");
const blogInfoService = require("@/server/blog_info");
const Redis = require("@/utils/redis");
const { KEYS } = require("./z_enter");

const comment = {
  async GetList(query) {
    const total = await commentDao.GetCount(query); //获取统计
    let list = [];
    if (total !== 0) {
      list = await commentDao.GetList(query);
    }
    return {
      pageSize: +query.page_size,
      pageNum: +query.page_num,
      pageData: list,
      total,
    };
  },
  async UpdateReview(body) {
    const maps = { is_review: body.is_review };
    await z_base_Dao.UpdatesMap(CommentModal.Comment, maps, { id: body.ids });
    return ErrorCode.OK;
  },
  async Delete(ids) {
    await z_base_Dao.Delete(CommentModal.Comment, { id: ids });
    return ErrorCode.OK;
  },
  async GetFrontList(query) {
    // 查询评论列表
    let { commentList, total } = await commentDao.GetFrontList(query);

    //统计评论列表(可以理解为第一层评论)
    const commentIds = commentList.map((item) => item.id);
    //统计点赞数
    const likeCountMap = await Redis.HGetAll(KEYS.KEY_COMMENT_LIKE_COUNT);

    //获取回复列表
    let replyList = await commentDao.GetReplyList(commentIds);

    let replyMap = {};

    replyList.forEach((reply) => {
      reply.likeCount = +likeCountMap[reply.id] || 0;
      if (!replyMap[reply.parent_id]) {
        replyMap[reply.parent_id] = [];
      }
      if (replyMap[reply.parent_id].length < 3) {
        replyMap[reply.parent_id].push(reply);
      }
    });

    // 获取评论的回复数量
    let replyCountList = await commentDao.GetReplyCountListByCommentId(
      commentIds
    );
    let replyCountMap = {}; //【回复id：回复数量】
    for (const reply of replyCountList) {
      //评论的id作为主键
      replyCountMap[reply.comment_id] = reply.reply_count;
    }

    for (let i = 0; i < commentList.length; i++) {
      let comment = commentList[i];
      //为评论添加点赞数量
      comment.like_count = +likeCountMap[comment.id] || 0;
      //为评论添加回复列表
      comment.reply_vo_list = replyMap[comment.id] || [];
      //为评论添加回复数量
      comment.reply_count = replyCountMap[comment.id] || 0;
      //为评论回复添加点赞数量
      for (let j = 0; j < comment.reply_vo_list.lengthl; j++) {
        let reply = comment.reply_vo_list[j];
        reply.like_count = +likeCountMap[reply.id] || 0;
      }
    }
    return {
      total: total,
      pageData: commentList,
      pageSize: +query.page_size,
      pageNum: +query.page_num,
    };
  },
  async Save(uid, req) {
    let comment = cloneDeep(req);
    comment.user_id = uid;
    //根据博客设置中的配置 设置默认是否审核
    const blog_config = await blogInfoService.GetBlogConfig();
    let is_comment_review = blog_config.is_comment_review;
    comment.is_review = is_comment_review;
    comment.created_at = new Date().toLocaleString();
    comment.updated_at = new Date().toLocaleString();
    comment.is_delete = 0;
    await z_base_Dao.Create(CommentModal.Comment, comment);
    return ErrorCode.OK;
  },
  async SaveLike(uid, commentId) {
    //记录用户对评论点赞
    const commentLikeUserKey = KEYS.KEY_COMMENT_USER_LIKE_SET + uid;
    const flag = await Redis.SIsMember(commentLikeUserKey, commentId);
    if (flag) {
      await Redis.SRem(commentLikeUserKey, commentId);
      await Redis.HIncrBy(
        KEYS.KEY_COMMENT_LIKE_COUNT,
        commentId.toString(),
        -1
      );
    } else {
      await Redis.SAdd(commentLikeUserKey, commentId);
      await Redis.HIncrBy(KEYS.KEY_COMMENT_LIKE_COUNT, commentId.toString(), 1);
    }
    return ErrorCode.OK;
  },
  async GetReplyListByCommentId(id, query = {}) {
    //query传递是分页数据，这里没有制作该功能
    let replyList = await commentDao.GetReplyListByCommentId(id); //query不传递了
    let likeCountMap = await Redis.HGetAll(KEYS.KEY_COMMENT_LIKE_COUNT);
    for (let i = 0; i < replyList.length; i++) {
      let reply = replyList[i];
      reply.like_count = +likeCountMap[reply.id];
    }
    return replyList;
  },
};

module.exports = comment;
